import _ from 'lodash'

export function formatMoney (text, isCent = true) {
  const num = Number(text)

  let divisor = 1

  if (isCent) {
    divisor = 100
  }

  if (isNaN(num)) return text

  const v = Math.round(num / divisor)

  return v.toLocaleString('en-US').concat('.').concat(v.toFixed(2).split('.')[1])
}

export function calcHouse ({
  // 总价
  hTotalPrice,

  // 建筑单价
  hosUnitPrice,
  // 建筑面积
  hosUnitArea,

  // 套内单价
  setInPrice,
  // 套内面积
  setInArea,

  // 定金
  stationaryMoney,
}) {
  let _hTotalPrice     = parseFloat(hTotalPrice),
      _hosUnitPrice    = parseFloat(hosUnitPrice),
      _hosUnitArea     = parseFloat(hosUnitArea),
      _setInPrice      = parseFloat(setInPrice),
      _setInArea       = parseFloat(setInArea),
      _stationaryMoney = parseFloat(stationaryMoney)

  if (_hosUnitPrice && _hosUnitArea) {
    if (!_hTotalPrice) {
      _hTotalPrice = _hosUnitPrice * _hosUnitArea
    }

    if (!_setInPrice && _setInArea) {
      _setInPrice = _hTotalPrice / _setInArea
    }

    if (_setInPrice && !_setInArea) {
      _setInArea = _hTotalPrice / _setInPrice
    }
  }

  if (_setInPrice && _setInArea) {
    if (!_hTotalPrice) {
      _hTotalPrice = _setInPrice * _setInArea
    }

    if (!_hosUnitPrice && _hosUnitArea) {
      _hosUnitPrice = _hTotalPrice / _hosUnitArea
    }

    if (_hosUnitPrice && !_hosUnitArea) {
      _hosUnitArea = _hTotalPrice / _hosUnitPrice
    }
  }

  return {
    hTotalPrice: formatMoney(_hTotalPrice / 10000).concat('万元'),

    hosUnitPrice: formatMoney(_hosUnitPrice).concat('元/㎡'),
    hosUnitArea: _hosUnitArea.toString().concat('㎡'),
    setInPrice: formatMoney(_setInPrice).concat('元/㎡'),
    setInArea: _setInArea.toString().concat('㎡'),
    stationaryMoney: _stationaryMoney ? formatMoney(_stationaryMoney).concat('元') : null,

    _hTotalPrice,
    _hosUnitPrice,
    _hosUnitArea,
    _setInPrice,
    _setInArea,
    _stationaryMoney
  }
}
